package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    @Transactional
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //当前商品是否在购物车已经存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());//设置用户id

        ShoppingCart shoppingCartlist =  shoppingCartMapper.list(shoppingCart); //查询

        //如果存在时，将商品数量加1
        if (shoppingCartlist != null){
            shoppingCartlist.setNumber(shoppingCartlist.getNumber()+1);
            shoppingCartMapper.updateNumber(shoppingCartlist);
        }else {
            //如果不存在时 在表中插入该数据
            Long dishId = shoppingCartDTO.getDishId();
            Long  setmealId  = shoppingCartDTO.getSetmealId();
            if (dishId !=null){
                //在购物车中插入菜品相关数据
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());


            }else {
                //在购物车中插入套餐相关数据
                SetmealVO setmealVO = setmealMapper.getByid(setmealId);
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setAmount(setmealVO.getPrice());
                shoppingCart.setImage(setmealVO.getImage());
            }
            shoppingCart.setNumber(0);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    /**
     * 查看购物车
     */
    @Override
    public List<ShoppingCart> getShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.getShoppingCart(userId);
        return shoppingCartList;
    }

    /**
     * 删除购物车中的一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {

            ShoppingCart shoppingCart = new ShoppingCart(); //创建对象
            BeanUtils.copyProperties(shoppingCartDTO,shoppingCart); //属性拷贝
            shoppingCart.setUserId(BaseContext.getCurrentId()); //设置用户id
            // 删除的是菜品 还是套餐 不用判断 因为他发过来的 只有一种情况
            shoppingCartMapper.deleteOne(shoppingCart);//使用动态sql


    }

    /**
     * 清空购物车
     */
    @Override
    public void deleteAll() {

            Long userId = BaseContext.getCurrentId();
            shoppingCartMapper.deleteAll(userId);
    }
}
